import config from '../../utils/config.js'
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js');
var app = getApp();

Page({
	data: {
		price: {},
		group: [],
		isAllChecked: false,
		isIndeterminate: false,
		totalPrice: 0
	},


	onLoad(options) {
        
		let price = JSON.parse(options.price);
		this.setData({ price });
	},

	groupChange(e) {
		let { price } = this.data;
		let group = e.detail;
		for (let i = 0; i < price.length; i++) {
			if (group.includes(i)) {
				price[i].checked = true;
			}else{
				price[i].checked = false;
			}
			this.setData({ group, price });
		};
	},
	groupAllChange(e) {
		this.setData({
			isAllChecked: e.detail.value,
			isIndeterminate: e.detail.indeterminate
		})
		this.addUp();
	},

	addUp() {
		let totalPrice = 0;
		let { price, group, isAllChecked } = this.data;
		if (isAllChecked) {
			totalPrice = price[0].price;
		} else {
			group.forEach(element => {
				totalPrice += price[element].price;
			}); 
			console.log({ totalPrice });
		} 

		this.setData({ totalPrice });
	},
	onReady() {
 
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})